﻿using System.Linq;
using Windows.Media.Playback;
using Windows.Phone.UI.Input;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;
using FunstreamTV.Common;
using FunstreamTV.Model.Chat;
using FunstreamTV.Model.Content;
using FunstreamTV.Tools;
using FunstreamTV.ViewModel;
using FunstreamTV.ViewModel.ObjectViewModel;

namespace FunstreamTV
{
    public sealed partial class StreamPage : Page
    {
        private ContentViewModel CurrentStream { get; set; }
        private NavigationHelper navigationHelper;
        public StreamViewModel StreamViewModel { get; set; }

        public StreamPage()
        {
            this.InitializeComponent();

            this.navigationHelper = new NavigationHelper(this);
            
        }


        public NavigationHelper NavigationHelper => this.navigationHelper;

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);
            DataContext = new StreamViewModel();
            StreamViewModel = (StreamViewModel)DataContext;
            StreamViewModel.MessageAdded += StreamViewModelMessageAdded;
            if (e.Parameter != null)
                CurrentStream = e.Parameter as ContentViewModel;

            await StreamViewModel.Load(CurrentStream);
        }

        private void StreamViewModelMessageAdded(object sender, System.EventArgs e)
        {
            ChatListView.ScrollIntoView(ChatListView.Items.Last());
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }

        private void SettingsClick(object sender, RoutedEventArgs e)
        {
            StreamViewModel.ShowPlayer();
        }

        private void ChatClick(object sender, RoutedEventArgs e)
        {
            StreamViewModel.ShowChat();
        }

        private void SmilesClick(object sender, RoutedEventArgs e)
        {
            if (StreamViewModel.SmilesVisibility != Visibility.Visible)
                StreamViewModel.ShowSmiles();
            else
                StreamViewModel.ShowChat();
        }

        private void MessageTextBoxKeyUp(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key.Equals(VirtualKey.Enter))
            {
                StreamViewModel.SendMessage(CurrentStream.streamer.id, (sender as TextBox).Text);
            }
        }

        private void SmileClick(object sender, ItemClickEventArgs e)
        {
            StreamViewModel.Message += ":" + (e.ClickedItem as SmileViewModel).code + ":";
            StreamViewModel.ShowChat();
        }

        private void SendClick(object sender, RoutedEventArgs e)
        {
            StreamViewModel.SendMessage(CurrentStream.streamer.id, StreamViewModel.Message);
        }

        private void ChatMessageClick(object sender, ItemClickEventArgs e)
        {
            StreamViewModel.Recipient = (e.ClickedItem as ChatMessageViewModel).from;
            StreamViewModel.RecipientVisibility = Visibility.Visible;
            ChatListView.ScrollIntoView(ChatListView.Items.Last());
        }

        private void RecipientRemove(object sender, TappedRoutedEventArgs e)
        {
            StreamViewModel.Recipient = null;
            StreamViewModel.RecipientVisibility = Visibility.Collapsed;
        }

        private void StreamerAsRecipient(object sender, RoutedEventArgs e)
        {
            StreamViewModel.Recipient = new ChatFrom { id = CurrentStream.streamer.id, name = CurrentStream.streamer.name };
            StreamViewModel.RecipientVisibility = Visibility.Visible;
            ChatListView.ScrollIntoView(ChatListView.Items.Last());
        }

        private void PlayerSelected(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Any())
            {
                var player = (e.AddedItems.First() as Player);
                
            }
        }

        private void QualitySelected(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Any())
            {
                var quality = (e.AddedItems.First() as QualityItem);
                
            }
        }
    }
}
